﻿import React from 'react';
import { createBrowserRouter } from 'react-router-dom';

import App from '../App';
import Ai from '../AiModule/Ai';
import SmartChat from '../AiModule/SmartChat';
import Gzt from '../WSC/Gzt';
import Login from '../WSC/Login';
import MeetingRoomWrapper from '../MeetingModule/MeetingRoomWrapper';
import Chat from '../MessageModule/Chat';
import Contacts from '../MessageModule/Contacts';
import TabBarTest from '../components/TabBarTest';

import HomeWrapper from './HomeWrapper';
import { XqWrapper } from './XqWrapper';

interface UserData {
  username: string;
  image: string;
  isLoggedIn: boolean;
}

// Chat页面的包装组件，用于提供用户信息
export const ChatWrapper: React.FC = () => {
  const loginUsername = localStorage.getItem('login_username');
  const loginType = localStorage.getItem('login_type');

  if (!loginUsername || !loginType) {
    // 如果没有登录信息，重定向到登录页面
    window.location.href = '/login';
    return null;
  }

  // 构造用户信息对象
  const userData: UserData = {
    username: loginUsername,
    image:
      localStorage.getItem('login_avatar') ||
      'https://img1.baidu.com/it/u=3217838212,795208401&fm=253&fmt=auto&app=138&f=JPEG?w=514&h=500',
    isLoggedIn: true,
  };

  return <Chat currentUser={userData} />;
};

// Contacts页面的包装组件，用于提供用户信息
const ContactsPageWrapper: React.FC = () => {
  const loginUsername = localStorage.getItem('login_username');
  const loginType = localStorage.getItem('login_type');

  if (!loginUsername || !loginType) {
    // 如果没有登录信息，重定向到登录页面
    window.location.href = '/login';
    return null;
  }

  // 构造用户信息对象
  const userData: UserData = {
    username: loginUsername,
    image:
      localStorage.getItem('login_avatar') ||
      'https://img1.baidu.com/it/u=3217838212,795208401&fm=253&fmt=auto&app=138&f=JPEG?w=514&h=500',
    isLoggedIn: true,
  };

  return <Contacts currentUser={userData} />;
};

// 路由配置
export const router = createBrowserRouter([
  {
    path: '/login',
    element: <Login />,
  },
  {
    path: '/test',
    element: <TabBarTest />,
  },
  // 会议室独立路由
  {
    path: '/MeetingRoomWrapper',
    element: <MeetingRoomWrapper />,
  },
  {
    path: '/meeting/:roomId',
    element: <MeetingRoomWrapper />,
  },
  // App及其子路由
  {
    path: '/',
    element: <App />,
    children: [
      {
        index: true,
        element: <HomeWrapper />,
      },
      {
        path: 'contacts',
        element: <ContactsPageWrapper />,
      },
      {
        path: 'message',
        element: <SmartChat />,
      },
      {
        path: 'me',
        element: <Gzt />,
      },
      // 其他路由
      {
        path: 'xq',
        element: <XqWrapper />,
      },
      {
        path: 'ai',
        element: <Ai />,
      },
    ],
  },
]);

export default router;
